import 'dart:io';

import 'package:dio_log/dio_log.dart';
import 'package:fk_user_agent/fk_user_agent.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_ffmpeg/statistics.dart';
import 'package:get/get.dart';
import 'package:package_info/package_info.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:star_author/env/env_config.dart';
import 'package:star_author/provider/utils/persist.dart';
import 'package:star_author/utils/common_util/shared_preferences/sp_util.dart';
import 'package:star_author/widgets/error_widget.dart';

import 'api/my_http_overrides.dart';
import 'app.dart';
import 'config/app.dart';
import 'controller/app_package_info_getx_controller.dart';

//初始化sentry
void initSentryAndApp() async {
  // HttpCryptUtil.requestCrypt();
  await initApp(); //初始化sp
  HttpOverrides.global = MyHttpOverrides();
  await SentryFlutter.init(
    (options) {
      options.dsn = EnvConfig.isProd() ? App.entryAnchor : App.entryAnchor;
    },
    appRunner: () {
      runApp(AppInit());
    },
  );
}

//配置
Future<void> initApp() async {
  await PersistInstance.init();

  await SpUtil.getInstance();


  await FkUserAgent.init();

  DioLogInterceptor.enablePrintLog = false;

  ///获取版本信息
  PackageInfo packageInfo = await PackageInfo.fromPlatform();
  AppPackageInfoGetXController appPackageInfoGetXController =
      Get.put(AppPackageInfoGetXController());
  appPackageInfoGetXController.setPackageInfo(packageInfo);
  // 强制竖屏
  WidgetsFlutterBinding.ensureInitialized();

  SystemChrome.setPreferredOrientations(
    [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown],
  );

  SystemChrome.setSystemUIOverlayStyle(
    SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarBrightness: Brightness.light,
    ),
  );
  //当页面会灰屏的时候 变成这个组件 页面就不会灰屏了 不会影响使用 只是部分ui不显示
  ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails) {
    return MyErrorWidget();
  };
}
